/*
 * SiFiSy - A File System Simulator
 * Copyright (C) 2010  SevenSoft
 *
 * This file is part of SiFiSy.
 *
 * SiFiSy is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * SiFiSy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SiFiSy.  If not, see <http://www.gnu.org/licenses/>.
 */
package sifisy.connections;

import com.itextpdf.text.DocumentException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.*;
import java.io.FileNotFoundException;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JRootPane;
import sifisy.core.*;
import sifisy.newgui.*;

/**
 * Class that control user inputs for single simulation
 * @author Alessandro Vedovato
 * @version %I%, %G%
 * @createData 17/02/2010
 */
public class SimulationManager extends Controller {

    private SimulationPanel panel;
    private boolean[] existError;
    private int simId;
    private long busySpace = 0;
    //private Vector<String> fileNameMemory = new Vector<String>();
    

    /**
     * Constructor
     * @param panel
     * @param name
     */
    public SimulationManager(SimulationPanel panel, String name) {
        this.panel = panel;
        panel.lockWindowsDialog(true);
        Model.newSimulation(name, panel);
        existError = new boolean[39];
        for (int i = 0; i < existError.length; i++) {
            existError[i] = false;
        }
    }

    /**
     * Method that contact view to write existent error from user imput
     */
    public void writeError() {
        panel.clearMessages();
        for (int i = 0; i < existError.length; i++) {
            if (existError[i] == true) {
                panel.setTextError(error[i]);
            }
        }
    }

    /**
     * Metod that call Model to export a simulation in a file pdf
     * with given Path
     * @param filePath
     */
    public void exportPdf(String filePath) {
        try {
            Model.exportSimulationPdf(panel, filePath);
        } catch (DocumentException e) {
            System.err.println(e.getMessage());
        }
    }

    /**
     * Metod that call Model to export a graphic in a file png with given Path
     * @param filePath
     */
    public void exportGraphicsPng(String filePath) {
        try {
            Model.exportSimulationPng((JRootPane) (((BorderLayout) panel.getFrameGraphics().getLayout()).getLayoutComponent(BorderLayout.CENTER)), filePath);
        } catch (FileNotFoundException e) {
            System.err.println(e.getMessage());
        }
    }

    /**
     * Methot that say if there are error in a simulation
     * @return
     */
    public boolean hasErrors() {
        boolean errors = false;

        for (int i = 0; i < existError.length && !errors; i++) {
            if (existError[i] == true) {
                errors = true;
            }
        }

        return errors;
    }

    /**
     * Inner class for the TextBox Partition
     */
    public class TestDimPart implements KeyListener {

        /**
         * Method that at the relise of a button checks the value added as
         * the partition size
         * @param event
         */
        public void keyReleased(KeyEvent event) {
            dimPart();
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyPressed(KeyEvent event) {
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyTyped(KeyEvent event) {
        }
    }

    /**
     * Inner class for swich File Sistem
     */
    public class SwitchFileSytemPanel implements ActionListener {

        /**
         * Methot that at the click of a button swich type of File System
         * @param e
         */
        public void actionPerformed(ActionEvent e) {
            //da settare i valori del block (fare get da panel)
            String actual = ((JComboBox) e.getSource()).getSelectedItem().toString();
            if (actual.equals("NTFS")) {
                panel.getNtfsFSType_c().setSelectedIndex(1);
                panel.getNtfsFSConfiguration().setVisible(true);
                panel.getExt2FSConfiguration().setVisible(false);
                panel.getFat32FSConfiguration().setVisible(false);
            }
            if (actual.equals("EXT2")) {
                panel.getExt2FSType_c().setSelectedIndex(0);
                panel.getNtfsFSConfiguration().setVisible(false);
                panel.getExt2FSConfiguration().setVisible(true);
                panel.getFat32FSConfiguration().setVisible(false);
            }
            if (actual.equals("FAT32")) {
                panel.getFat32FSType_c().setSelectedIndex(2);
                panel.getNtfsFSConfiguration().setVisible(false);
                panel.getExt2FSConfiguration().setVisible(false);
                panel.getFat32FSConfiguration().setVisible(true);
            }

            model.calculateMaxLenghtFile(simId);
            long maxLenght = model.getMaxLenghtFile(simId);
            String stringFileExp = panel.getComboDimFile();
            setMaxFile(maxLenght, stringFileExp);

            changeFileSystemType(actual);
        }
    }

    /**
     * Inner class for the TextBox Partition
     */
    public class TestComboDimPart implements ItemListener {

        /**
         * Method that at the lost of focus checks the value
         * added as Partition dimension
         * @param event
         */
        public void itemStateChanged(ItemEvent event) {
            //itemStateChanged(ItemEvent event) viene chiamato quando viene
            //selezionato o deselezionato con i seguenti valori
            //SELECTED == 1
            //DESELECTED == 2
            int eventType = event.getStateChange();
            if (eventType == 1) {
                dimPart();
            }
        }
    }

    /**
     * Inner class for the TextBox Block
     */
    public class TestDimBlock implements KeyListener {

        /**
         * Method that at the relise of a button checks the value added as
         * the block size
         * @param event
         */
        public void keyReleased(KeyEvent event) {
            dimBlock();
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyPressed(KeyEvent event) {
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyTyped(KeyEvent event) {
        }
    }

    /**
     * Inner class for the TextBox Block
     */
    public class TestComboDimBlock implements ItemListener {

        /**
         * Method that at the lost of focus checks the value added as Block
         * dimension
         * @param event
         */
        public void itemStateChanged(ItemEvent event) {
            //itemStateChanged(ItemEvent event) viene chiamato quando viene
            //selezionato o deselezionato con i seguenti valori
            //SELECTED == 1
            //DESELECTED == 2
            int eventType = event.getStateChange();
            if (eventType == 1) {
                dimBlock();
            }
        }
    }

    /**
     * Inner class for the TextBox File
     */
    public class TestDimFile implements KeyListener {

        /**
         * Method that at the relise of a button checks the value added as
         * the file size
         * @param event
         */
        public void keyReleased(KeyEvent event) {
            dimFile();
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyPressed(KeyEvent event) {
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyTyped(KeyEvent event) {
        }
    }

    /**
     * Inner class for the TextBox File
     */
    public class TestComboDimFile implements ItemListener {

        /**
         * Method that at the lost of focus checks the value added as File
         * dimension
         * @param event
         */
        public void itemStateChanged(ItemEvent event) {
            //itemStateChanged(ItemEvent event) viene chiamato quando viene
            //selezionato o deselezionato con i seguenti valori
            //SELECTED == 1
            //DESELECTED == 2
            int eventType = event.getStateChange();
            if (eventType == 1) {
                dimFile();
            }
        }
    }

    /**
     * Inner class for the TextBox MultiFile
     */
    public class TestDimMultiFile implements KeyListener {

        /**
         * Method that at the relise of a button checks the value added as the
         * multi file size
         * @param event
         */
        public void keyReleased(KeyEvent e) {
            dimMultiFile();
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyPressed(KeyEvent e) {
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyTyped(KeyEvent e) {
        }
    }

    /**
     * Inner class for the TextBox MultiFile
     */
    public class TestComboDimMultiFile implements ItemListener {

        /**
         * Method that at the lost of focus checks the value added as
         * File dimension
         * @param event
         */
        public void itemStateChanged(ItemEvent event) {
            //itemStateChanged(ItemEvent event) viene chiamato quando viene
            //selezionato o deselezionato con i seguenti valori
            //SELECTED == 1
            //DESELECTED == 2
            int eventType = event.getStateChange();
            if (eventType == 1) {
                dimMultiFile();
            }
        }
    }

    /**
     * Inner class for the TextBox Fragmentation
     */
    public class TestFragmentation implements KeyListener {

        /**
         * Method that at the relise of a button checks the value added as
         * the file fragmentation
         * @param event
         */
        public void keyReleased(KeyEvent event) {
            simId = panel.getSimId();
            existError[28] = false; //Il numero inserito dovrebbe esser corretto
            try {
                int average = Integer.parseInt(panel.getTextFragmentation().
                        getText());
                if (average > 0) {
                    existError[6] = false; // frammentazione valida
                    panel.getTextFragmentation().setForeground(Color.black);
                } else {
                    panel.getTextFragmentation().setForeground(Color.red);
                    existError[6] = true; // frammentazione non valida
                }

            } catch (NumberFormatException e) {
                existError[28] = true; //il valore inserito non è un numero
                existError[6] = false; //non è un numero quindi non puo essere <0
                panel.getTextFragmentation().setForeground(Color.red);
            }
            writeError();
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyPressed(KeyEvent event) {
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyTyped(KeyEvent event) {
        }
    }

//-----------------------------------------------------------CONTROLLO INODE
    /**
     * Inner class for the TextBox First i-Node
     */
    public class TestDirectBlockFirstiNode implements KeyListener {

        /**
         * Method that at the relise of a button checks the value added as the
         * number of blocks of first i-Node
         * @param event
         */
        public void keyReleased(KeyEvent event) {
            simId = panel.getSimId();
            existError[29] = false;
            try {
                int blockFirstInode = Integer.parseInt(panel.getTextDirectBlockFirstiNode().getText());
                if (blockFirstInode <= 12) {
                    if (blockFirstInode > 0) {
                        existError[7] = false; // primo i-node corretto
                        model.setBlockFirstInode(simId, blockFirstInode);
                        panel.getTextDirectBlockFirstiNode().
                                setForeground(Color.black);

                        checkFile();
//                        model.calculateMaxLenghtFile(simId);
//                        long maxLenght = model.getMaxLenghtFile(simId);
//                        String stringFileExp = panel.getComboDimFile();
//                        setMaxFile(maxLenght, stringFileExp);

                    } else {
                        existError[8] = false; // primo i-node corretto
                        existError[7] = true; // valore <0
                        panel.getTextDirectBlockFirstiNode().
                                setForeground(Color.red);
                    }
                    existError[8] = false; // primo i-node corretto
                } else {
                    existError[7] = false; //primo i-node corretto
                    existError[8] = true; //valore troppo grande
                    panel.getTextDirectBlockFirstiNode().
                            setForeground(Color.red);
                }
            } catch (NumberFormatException e) {
                existError[29] = true; //il valore inserito non è un numero
                existError[7] = false; //non è un numero, non puo essere <0
                existError[8] = false; //non è un numero, non puo essere troppo grande
                panel.getTextDirectBlockFirstiNode().setForeground(Color.red);
            }
            writeError();
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyPressed(KeyEvent event) {
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyTyped(KeyEvent event) {
        }
    }

    /**
     * Inner class for the TextBox i-Node
     */
    public class TestDimiNode implements KeyListener {

        /**
         * Method that at the relise of a button checks the value added as the
         * i-Node dimension
         * @param event
         */
        public void keyReleased(KeyEvent event) {
            simId = panel.getSimId();
            existError[30] = false; //il valore inserito dovrebbe essere un numero
            try {
                int inode = Integer.parseInt(panel.getTextDimiNode().getText());
                if (inode <= 128) {
                    if (inode > 0) {
                        existError[9] = false; // valore corretto
                        model.setInodeDimension(simId, inode);
                        panel.getTextDimiNode().setForeground(Color.black);

                        checkFile();
//                        model.calculateMaxLenghtFile(simId);
//                        long maxLenght = model.getMaxLenghtFile(simId);
//                        String stringFileExp = panel.getComboDimFile();
//                        setMaxFile(maxLenght, stringFileExp);

                    } else {
                        panel.getTextDimiNode().setForeground(Color.red);
                        existError[10] = false; // valore corretto
                        existError[9] = true; // valore <0
                    }
                    existError[10] = false; // valore corretto
                } else {
                    panel.getTextDimiNode().setForeground(Color.red);
                    existError[9] = false; // valore corretto
                    existError[10] = true; // valore troppo grande
                }
            } catch (NumberFormatException e) {
                existError[30] = true; //il valore inserito non è un numero
                existError[9] = false; //non è un numero quindi non puo essere <0
                existError[10] = false; //non è un numero quindi non puo essere troppo grande
                panel.getTextDimiNode().setForeground(Color.red);
            }
            writeError();
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyPressed(KeyEvent event) {
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyTyped(KeyEvent event) {
        }
    }

    //-------------------------------------------------------CONTROLLO INODE END
    //------------------------------------------------SELEZIONE TIPO FILE SYSTEM
    /**
     * Inner class that contact Model for change Fyle System Type
     */
    public class TestComboFSType implements ItemListener {

        /**
         * Method that when an item is selected checks the value added as
         * File System type
         * @param event
         */
        public void itemStateChanged(ItemEvent event) {
            //itemStateChanged(ItemEvent event) viene chiamato quando viene
            //selezionato o deselezionato con i seguenti valori
            //SELECTED == 1
            //DESELECTED == 2
            int eventType = event.getStateChange();
            if (eventType == 1) {
                simId = panel.getSimId();
                String fs = panel.getComboFSType();
                model.switchFileSystem(simId, fs);
            }
        }
    }
    //--------------------------------------------SELEZIONE TIPO FILE SYSTEM END

    /**
     * Inner class for the TextBox Maximum Address Level
     */
    public class TestComboMaxAddressLevel implements ItemListener {

        /**
         * Method that when an item is selected checks the value added as
         * maximum address level
         * @param event
         */
        public void itemStateChanged(ItemEvent event) {
            //itemStateChanged(ItemEvent event) viene chiamato quando viene
            //selezionato o deselezionato con i seguenti valori
            //SELECTED == 1
            //DESELECTED == 2
            int eventType = event.getStateChange();
            if (eventType == 1) {
                simId = panel.getSimId();
                int addressLevel = Integer.parseInt(panel.getComboMaxAddressLevel());
                model.setMaxAddressLevel(simId, addressLevel);

                checkFile();
//                model.calculateMaxLenghtFile(simId);
//                long maxLenght = model.getMaxLenghtFile(simId);
//                String stringFileExp = panel.getComboDimFile();
//                setMaxFile(maxLenght, stringFileExp);
            }
        }
    }
    //--------------------------------------------------------CONTROLLO AVANZATE

    /**
     * Inner class that show or hide advanced camps
     */
    public class ShowHideAdvancedPanel implements MouseListener {

        /**
         * Method that at the click of a button show or hide advanced camps 
         * @param e
         */
        public void mouseClicked(MouseEvent e) {
            if (panel.getHdAdvancedButton().getText().equals(Model.translate("advanced"))) {
                panel.getHdAdvancedConfiguration().setVisible(true);
                model.setChooseAdvanced(panel.getSimId(), true);
                panel.getHdAdvancedButton().setText("Standard");
            } else {
                panel.getHdAdvancedConfiguration().setVisible(false);
                panel.getHdAdvancedButton().setText(Model.translate("advanced"));
                model.setChooseAdvanced(panel.getSimId(), false);
            }
        }

        /**
         * Method for the implementation of the MouseListener
         * @param event
         */
        public void mousePressed(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        /**
         * Method for the implementation of the MouseListener
         * @param event
         */
        public void mouseReleased(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        /**
         * Method for the implementation of the MouseListener
         * @param event
         */
        public void mouseEntered(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        /**
         * Method for the implementation of the MouseListener
         * @param event
         */
        public void mouseExited(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    /**
     * Inner class for the TextBox Rotation Speed
     */
    public class TestRotationSpeed implements KeyListener {

        /**
         * Method that at the relise of a button checks the value added as the
         * rotation speed
         * @param event
         */
        public void keyReleased(KeyEvent event) {
            simId = panel.getSimId();
            existError[31] = false; // il valore inserito dovrebbe essere un numero
            try {
                int rotationSpeed = Integer.parseInt(panel.getTextRotationSpeed().getText());
                if (rotationSpeed <= 32000) {
                    if (rotationSpeed > 0) {
                        existError[11] = false; // valore corretto
                        model.setRotationSpeed(simId, rotationSpeed);
                        panel.getTextRotationSpeed().setForeground(Color.black);
                    } else {
                        panel.getTextRotationSpeed().setForeground(Color.red);
                        existError[11] = true; // valore <0
                    }
                    existError[12] = false; // valore corretto
                } else {
                    panel.getTextRotationSpeed().setForeground(Color.red);
                    existError[11] = false; // valore corretto
                    existError[12] = true; // valore troppo grande
                }
            } catch (NumberFormatException e) {
                existError[31] = true; //il valore inserito non è un numero
                existError[11] = false; //non è un numero, non puo essere <0
                existError[12] = false; //non è un numero, non puo essere troppo grande
                panel.getTextRotationSpeed().setForeground(Color.red);
            }
            writeError();
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyPressed(KeyEvent event) {
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyTyped(KeyEvent event) {
        }
    }

    /**
     * Inner class for the TextBox Seek Time
     */
    public class TestSeekTime implements KeyListener {

        /**
         * Method that at the relise of a button checks the value added as the
         * Seek Time
         * @param event
         */
        public void keyReleased(KeyEvent event) {
            simId = panel.getSimId();
            existError[32] = false; // il valore inserito dovrebbe essere un numero
            try {
                int seekTime = Integer.parseInt(panel.getTextSeekTime().getText());
                if (seekTime <= 100) {
                    if (seekTime > 0) {
                        existError[13] = false; // valore corretto
                        model.setSeekTime(simId, seekTime);
                        panel.getTextSeekTime().setForeground(Color.black);
                    } else {
                        panel.getTextSeekTime().setForeground(Color.red);
                        existError[13] = true; // valore <0
                    }
                    existError[14] = false; // valore corretto
                } else {
                    panel.getTextSeekTime().setForeground(Color.red);
                    existError[13] = false; // valore corretto
                    existError[14] = true; // valore troppo grande
                }
            } catch (NumberFormatException e) {
                existError[32] = true; //il valore inserito non è un numero
                existError[13] = false; //non è un numero quindi non puo essere <0
                existError[14] = false; //non è un numero quindi non puo essere troppo grande
                panel.getTextSeekTime().setForeground(Color.red);
            }
            writeError();
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyPressed(KeyEvent event) {
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyTyped(KeyEvent event) {
        }
    }

    /**
     * Inner class for the TextBox Track
     */
    public class TestTrack implements KeyListener {

        /**
         * Method that at the lost of focus checks the value added as the Track
         * @param event
         */
        public void keyReleased(KeyEvent event) {
            simId = panel.getSimId();
            existError[33] = false; // il valore inserito dovrebbe essere un numero
            try {
                int track = Integer.parseInt(panel.getTextTrack().getText());
                //         int blockFileTrack = Integer.parseInt(panel.getTextBlockFileTrack().getText());
                if (track <= 1000) {
                    if (track > 0) {
                        existError[15] = false; // valore corretto
                        model.setTrack(simId, track);
                        panel.getTextTrack().setForeground(Color.black);
                    } else {
                        panel.getTextTrack().setForeground(Color.red);
                        existError[15] = true; // valore <0
                    }
                    existError[16] = false; // valore corretto
                } else {
                    panel.getTextTrack().setForeground(Color.red);
                    existError[15] = false; // valore corretto
                    existError[16] = true; // valore troppo grande
                }

                existError[18] = false; // non esiste mai, controllo rimosso
//                if (blockFileTrack >= track) {
//                    existError[18] = true; // valore troppo grande
//                    panel.getTextBlockFileTrack().setForeground(Color.red);
//                } else {
//                    existError[18] = false; // valore troppo grande
//                    panel.getTextBlockFileTrack().setForeground(Color.black);
//                }

            } catch (NumberFormatException e) {
                existError[33] = true; //il valore inserito non è un numero
                existError[15] = false; //non è un numero quindi non puo essere <0
                existError[16] = false; //non è un numero quindi non puo essere troppo grande
                panel.getTextTrack().setForeground(Color.red);
            }
            writeError();
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyPressed(KeyEvent event) {
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyTyped(KeyEvent event) {
        }
    }

//    public class TestBlockFileTrack implements KeyListener {
//
//        /**
//         * method that at the lost of focus checks the value added as the
//         * rotation speed
//         * @param event
//         */
//        public void keyReleased(KeyEvent event) {
//            simId = panel.getSimId();
//            existError[34] = false; // il valore inserito dovrebbe essere un numero
//            try {
//                int blockFileTrack = Integer.parseInt(panel
//                        .getTextBlockFileTrack().getText());
//                int track = Integer.parseInt(panel.getTextTrack().getText());
//                if (blockFileTrack <= track) {
//                    if (blockFileTrack > 0) {
//                        existError[17] = false; // valore corretto
//                        model.setBlockFileTrack(simId, blockFileTrack);
//                        panel.getTextBlockFileTrack().setForeground(Color.black);
//                    } else {
//                        panel.getTextBlockFileTrack().setForeground(Color.red);
//                        existError[18] = true; // valore <0
//                    }
//                    existError[18] = false; // valore corretto
//                } else {
//                    panel.getTextBlockFileTrack().setForeground(Color.red);
//                    existError[17] = false; // valore corretto
//                    existError[18] = true; // valore troppo grande
//                }
//            } catch (NumberFormatException e) {
//            existError[34] = true; //il valore inserito non è un numero
//            existError[17] = false; //non è un numero,non puo essere <0
//            existError[18] = false; //non è un numero, non puo essere troppo grande
//            panel.getTextBlockFileTrack().setForeground(Color.red);
//            }
//            writeError();
//        }
//
//        /**
//         * method for the implementation of the KeyListener
//         * @param event
//         */
//        public void keyPressed(KeyEvent event) {
//        }
//
//        /**
//         * method for the implementation of the KeyListener
//         * @param event
//         */
//        public void keyTyped(KeyEvent event) {
//        }
//    }
    //----------------------------------------------------CONTROLLO AVANZATE END
    //------------------------------------------------------------CONTROLLO NTFS
    /**
     * Inner class for the TextBox Main Record
     */
    public class TestMainRecord implements KeyListener {

        /**
         * Method that at the lost of focus checks the value added as the
         * main record
         * @param event
         */
        public void keyReleased(KeyEvent event) {
            simId = panel.getSimId();
            existError[35] = false; //si presume dato corretto
            try {
                int mainRecord = (Integer.parseInt(panel.getTextMainRecord().getText())) * 8; //trasformo il valore da bit in Byte
                int block;
                try {
                    block = Integer.parseInt(panel.getTextDimBlock().getText());
                } catch (NumberFormatException e) {
                    block = -1;
                }
                int blockExp = convertSize(panel.getComboDimBlock());
                long blockSize = dimensionPow(block, blockExp);
                if (mainRecord < blockSize) {
                    if (mainRecord > 0) {
                        existError[19] = false; // valore corretto 
                        model.setMainRecord(simId, mainRecord);
                        panel.getTextMainRecord().setForeground(Color.black);
                    } else {
                        panel.getTextMainRecord().setForeground(Color.red);
                        existError[19] = true; // valore <0 
                    }
                    existError[20] = false; // valore corretto 
                } else {
                    panel.getTextMainRecord().setForeground(Color.red);
                    existError[19] = false; // valore corretto 
                    existError[20] = true; // valore troppo grande 
                }
            } catch (NumberFormatException e) {
                existError[35] = true; //il valore inserito non è un numero 
                existError[19] = false; //non è un numero, non puo essere <0
                existError[20] = false; //non è un numero, non puo essere troppo grande
                panel.getTextMainRecord().setForeground(Color.red);
            }
            writeError();
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyPressed(KeyEvent event) {
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyTyped(KeyEvent event) {
        }
    }

    /**
     * Inner class for the TextBox Extension Record
     */
    public class TestExtensionRecord implements KeyListener {

        /**
         * Method that at the lost of focus checks the value added as
         * the main record
         * @param event
         */
        public void keyReleased(KeyEvent event) {
            simId = panel.getSimId();
            existError[36] = false; //si presume dato corretto
            try {
                int extensionRecord = (Integer.parseInt(panel.getTextExtensionRecord().getText())) * 8;
                //trasformo il valore da bit in Byte
                int block;
                try {
                    block = Integer.parseInt(panel.getTextDimBlock().getText());
                } catch (NumberFormatException e) {
                    block = -1;
                }
                int blockExp = convertSize(panel.getComboDimBlock());
                long blockSize = dimensionPow(block, blockExp);
                if (extensionRecord < blockSize) {
                    if (extensionRecord > 0) {
                        existError[21] = false; // valore corretto 
                        model.setExtensionRecord(simId, extensionRecord);
                        panel.getTextExtensionRecord().setForeground(Color.black);
                    } else {
                        panel.getTextExtensionRecord().setForeground(Color.red);
                        existError[21] = true; // valore <0 
                    }
                    existError[22] = false; // valore corretto 
                } else {
                    panel.getTextExtensionRecord().setForeground(Color.red);
                    existError[21] = false; // valore corretto 
                    existError[22] = true; // valore troppo grande 
                }
            } catch (NumberFormatException e) {
                existError[36] = true; //il valore inserito non è un numero 
                existError[21] = false; //non è un numero quindi non puo essere <0 
                existError[22] = false; //non è un numero quindi non puo essere troppo grande 
                panel.getTextExtensionRecord().setForeground(Color.red);
            }
            writeError();
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyPressed(KeyEvent event) {
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyTyped(KeyEvent event) {
        }
    }
    //--------------------------------------------------------CONTROLLO NTFS END

    /**
     * Inner class for the TextBox File Name
     */
    public class TestFileName implements KeyListener {

        /**
         * Method that at the lost of focus checks the value added
         * as the File Name
         * @param event
         */
        public void keyReleased(KeyEvent event) {
            String name = panel.getFileName().getText();
            boolean exist = false;
            for (int index = 0; index < panel.getInsertedFileTable().
                    getRowCount() && !exist; index++) {
                if (name.equals(panel.getInsertedFileTable().
                        getValueAt(index, 0))) {
                    exist = true;
                }
            }
            if (exist) {
                panel.btAddFileSetEnabled(false);
                panel.getFileName().setForeground(Color.red);
            } else {
                panel.btAddFileSetEnabled(validMultiFile());
                panel.getFileName().setForeground(Color.black);
            }
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyPressed(KeyEvent event) {
        }

        /**
         * Method for the implementation of the KeyListener
         * @param event
         */
        public void keyTyped(KeyEvent event) {
        }
    }

    /**
     * Inner class that call Model to start a simulation
     */
    public class StartSimulation implements ActionListener {

        /**
         * Method thar at the click of a button call Model to start a simulation
         * @param event
         */
        public void actionPerformed(ActionEvent event) {
            boolean ok = true;
            for (int i = 0; i < existError.length && ok; i++) {
                if (existError[i]) {
                    ok = false;
                }
            }

            if (ok) {
                panel.lockWindowsDialog(true);
                simId = panel.getSimId();
                if (panel.singleInsertType()) {
                    int average = Integer.parseInt(panel.getTextFragmentation().getText());
                    int file = Integer.parseInt(panel.getTextDimFile().getText());
                    int fileExp = convertSize(panel.getComboDimFile());
                    model.setFile(simId, "Singol", file, fileExp, average);
                }
                model.startSimulation(simId, panel);

                ///////////////////////
                (SimulationManager.this).panel.showResults();
                ///////////////////////
            } else {
                System.err.println("La simulazione contiene errori");
            }
        }
    }
    //-------------------------------------------------------end StartSimulation

    /**
     * Inner class that call Model to return to Default Simulation
     */
    public class ResetToDefault implements ActionListener {

        /**
         * Method that at the click of a button call Model to return
         * to Default Simulation
         * @param event
         */
        public void actionPerformed(ActionEvent event) {
            model.resetToDefault(simId, panel);
            (SimulationManager.this).panel.clearFields();
            (SimulationManager.this).panel.readSimulationValues();
            panel.getHdAdvancedConfiguration().setVisible(false);
            panel.getHdAdvancedButton().setText("Avanzate");
            model.setChooseAdvanced(panel.getSimId(), false);
            for (int i = 0; i < existError.length; i++) {
                existError[i] = false;
            }
        }
    }

    /**
     * Inner class that call Model to set as default users imput
     */
    public class TestSetAsDefault implements ActionListener {

        /**
         * Method that at the click of a button call Model to set
         * as default user imput
         * @param event
         */
        public void actionPerformed(ActionEvent event) {
            int simId = panel.getSimId();
            boolean ok = true;
            for (int i = 0; i < existError.length; i++) {
                if (existError[i]) {
                    ok = false;
                }
            }
            if (ok) {
                model.saveDefaultSimulation(simId, panel);
            }
        }
    }

    /**
     * Inner class that call Model to swich the file allocation type
     */
    public class TestComboAllocationType implements ItemListener {

        /**
         * Method that at the click of a button call Model to swich
         * the file allocation type
         * @param event
         */
        public void itemStateChanged(ItemEvent event) {
            //itemStateChanged(ItemEvent event) viene chiamato quando viene selezionato o deselezionato con i seguenti valori
            //SELECTED == 1
            //DESELECTED == 2
            int eventType = event.getStateChange();
            if (eventType == 1) {
                String allocationType = panel.getComboFileInsPolicy();
                model.setAllocationType(simId, allocationType);
            }
        }
    }

    /**
     * Method that checks the partition size and consistency with the block
     * ATTENZIONE: richiamare anche al cambio di esponente di block e partition
     */
    public void dimPart() {
        /**
         * deve:
         * controllare che dim partizione non sia troppo piccola rispetto ai blocchi decisi
         * controllare che il file scelto ci stia
         * controllare che il file non vada oltre il maxFile
         * controllare che non inserisca valori non numerici
         */
        simId = panel.getSimId();
        existError[25] = false; // il valore inserito dovrebbe essere un numero
        String exp = panel.getComboDimMultiFile();
//        boolean correctValue = false;
//        boolean consistence = false;
//        boolean consistenceFile = false;
//        boolean consistenceMultiFile = false;

        int blockDim = -1;
        int fileDim = -1;
        int partitionDim = -1;
        try {
            blockDim = Integer.parseInt(panel.getTextDimBlock().getText());
        } catch (NumberFormatException e) {
            blockDim = -1;
        }
        try {
            fileDim = Integer.parseInt(panel.getTextDimFile().getText());
        } catch (NumberFormatException e) {
            fileDim = -1;
        }

        try {
            partitionDim = Integer.parseInt(panel.getTextDimPart().getText());
        } catch (NumberFormatException e) {
            partitionDim = -1;
            existError[25] = true; //il valore inserito non è un numero
            existError[0] = false; //non è un numero quindi non puo essere <0
            existError[1] = false; //non è un numero quindi non puo essere troppo grande
            panel.getTextDimPart().setForeground(Color.red);
            panel.setLabelFreeDiskSpace("--", exp);
            panel.btAddFileSetEnabled(false);
        }

        int partitionExp = convertSize(panel.getComboDimPart());
        String stringFileExp = panel.getComboDimFile();
        int fileExp = convertSize(stringFileExp);
        int blockExp = convertSize(panel.getComboDimBlock());

        if (partitionDim != -1 && fileDim != -1 && blockDim != -1) {
                            
            if (partitionDim <= 0) {
                existError[0] = true;
                existError[1] = false;
                panel.getTextDimPart().setForeground(Color.red);
            } else {
                existError[0] = false;
                panel.getTextDimPart().setForeground(Color.black);
                //ho certamente dati numerici validi
                //controllo che siano consistenti tra di loro
                int addressBitPartition = Simulation.getNearestExponentOfTwo(fileDim);
                addressBitPartition = addressBitPartition + partitionExp;

                int addressBitBlock = Simulation.getNearestExponentOfTwo(blockDim);
                addressBitBlock = addressBitBlock + blockExp;

                if (addressBitPartition - addressBitBlock < 8) {
                    //sta cercando di usare un indirizzamento < 1 (espresso in byte)
                    //ci manda in tega tutti i calcoli!
                    existError[23] = true; //il blocco è troppo grande
                    panel.getTextDimBlock().setForeground(Color.red);
                } else {
                    //blocco e partizione buoni, setta l'address e controlla il max file
                    existError[23] = false;
                    panel.getTextDimBlock().setForeground(Color.black);
                    //(controllo bound massimi calcolabili)
                    if ((partitionExp == 10 && partitionDim <= 1024)
                            || (partitionExp == 20 && partitionDim <= 1024)
                            || (partitionExp == 30 && partitionDim <= 1024)
                            || (partitionExp == 40 && partitionDim <= 2)) {
//                        correctValue = true;
                        existError[1] = false; //non sono presenti errori di dimensione
                        panel.getTextDimPart().setForeground(Color.black);
                        //calcolo max file: sta usando inserimento singolo file o
                        //inserimento multiplo?
                        long partitionSize = dimensionPow(partitionDim, partitionExp);

                        if (panel.singleInsertType()) {
                            //il file è maggiore della partizione?
                            //il file è maggiore del max file calcolabile?
                            long fileSize = dimensionPow(fileDim, fileExp);

                            model.setPartition(simId, partitionDim);
                            model.setPartitionExponent(simId, partitionExp);
                            model.calculateMaxLenghtFile(simId);

                            long maxLenght = model.getMaxLenghtFile(simId);
                            if(maxLenght >= partitionSize)
                                maxLenght=partitionSize*3/4;
                            setMaxFile(maxLenght, stringFileExp);

                             //se il max file è maggiore della partizione...
                            if(maxLenght > partitionSize) maxLenght = partitionSize;
                            if (maxLenght >= fileSize) {
                                existError[38] = false; //il valore del file è coerente con la partizione <--------------24
                                panel.getTextDimPart().setForeground(Color.black);
                                panel.getTextDimFile().setForeground(Color.black);

                                setMaxFile(maxLenght, panel.getComboDimFile());

//                                consistenceFile = true;

                            } else {
                                existError[38] = true; //il file è troppo grande <--------------------------------24
                                panel.getTextDimPart().setForeground(Color.red);
                                panel.getTextDimFile().setForeground(Color.red);
                            }
                        } else {//inserimento multiplo

                            int multiFile;
                            try {
                                multiFile = Integer.parseInt(panel.getTextDimMultiFile().getText());
                            } catch (NumberFormatException e) {
                                multiFile = -1;
                            }
                            int multiFileExp = convertSize(panel.getComboDimMultiFile());
                            long multiFileSize = dimensionPow(multiFile, multiFileExp);
                            if (partitionSize > busySpace) {
                                if (partitionSize - busySpace > multiFileSize) {
                                    existError[37] = false;
                                    panel.getTextDimPart().setForeground(Color.black);
                                    if (multiFile != -1) {
                                        panel.getTextDimMultiFile().setForeground(Color.black);
                                    }
                                    panel.btAddFileSetEnabled(validMultiFile());
//                                    consistenceMultiFile = true;
                                    setFreeSpace(exp);
                                } else {
                                    existError[37] = true;
                                    panel.getTextDimPart().setForeground(Color.red);
                                }
                                existError[24] = false; //il valore del file è coerente con la partizione
                            } else {
                                existError[24] = true; //il file è troppo grande
                                panel.getTextDimPart().setForeground(Color.red);
                                panel.setLabelFreeDiskSpace("--", exp);
                            }

                        }//fine inserimento multiplo
                    } else {
                        //ha inserito valori fuori dai bound calcolabili
                        panel.getTextDimPart().setForeground(Color.red);
                        existError[1] = true; //numero inserito troppo grande
                        existError[0] = false; //non sono presenti errori di dimensione
                    }
                }//calcolato e settato il max file, la dimensione del file è coerente
                //con quella della partizione
            }
        }
        writeError();
    }

    /**
     * Method that checks the block size and consistency with the partition
     */
    public void dimBlock() {
        simId = panel.getSimId();
        existError[26] = false; // il valore inserito dovrebbe essere un numero
        try {
            int partition;
            try {
                partition = Integer.parseInt(panel.getTextDimPart().getText());
            } catch (NumberFormatException e) {
                partition = -1;
            }
            int block = Integer.parseInt(panel.getTextDimBlock().getText());
            boolean correctValue = false;
            boolean consistence = false;
            int blockExp = convertSize(panel.getComboDimBlock());
            int partExp = convertSize(panel.getComboDimPart());
            if ((blockExp == 10 && block <= 1024)
                    || (blockExp == 20 && block <= 1024)
                    || (blockExp == 30 && block <= 1024)
                    || (blockExp == 40 && block <= 2)) {
                if (block > 0) {
                    correctValue = true;
                    existError[2] = false; //il valore inserito è corretto
                } else {
                    existError[2] = true; //valore inserito <0
                }
                existError[3] = false; //il valore inserito è corretto
            } else {
                existError[3] = true; //valore inserito troppo grande
                existError[2] = false; //il valore inserito è corretto
            }
            long partSize = dimensionPow(partition, partExp);
            long blockSize = dimensionPow(block, blockExp);
            if (partSize > blockSize) {
                existError[23] = false;
                consistence = true;
                panel.getTextDimBlock().setForeground(Color.black);
                panel.getTextDimPart().setForeground(Color.black);
            } else {
                existError[23] = true;
                panel.getTextDimBlock().setForeground(Color.red);
                panel.getTextDimPart().setForeground(Color.red);
            }
            if (correctValue && consistence) {
                existError[38] = false;
                panel.getTextDimFile().setForeground(Color.black);
                model.setBlock(simId, block);
                model.setBlockExponent(simId, blockExp);
                panel.getTextDimPart().setForeground(Color.black);
                panel.getTextDimBlock().setForeground(Color.black);

                model.calculateMaxLenghtFile(simId);
                long maxLenght = model.getMaxLenghtFile(simId);

                int file = Integer.parseInt(panel.getTextDimFile().getText());
                String stringFileExp = panel.getComboDimFile();
                int fileExp = convertSize(stringFileExp);

                long fileDim = dimensionPow(file, fileExp);
                if (fileDim > maxLenght) {
                    existError[38] = true;
                    panel.getTextDimBlock().setForeground(Color.red);
                    panel.getTextDimPart().setForeground(Color.red);
                    panel.getTextDimFile().setForeground(Color.red);
                } else {
                    existError[38] = false;
                    panel.getTextDimBlock().setForeground(Color.black);
                    panel.getTextDimPart().setForeground(Color.black);
                    panel.getTextDimFile().setForeground(Color.black);
                }

                if(maxLenght > partSize) maxLenght = partSize;
                setMaxFile(maxLenght, stringFileExp);

            } else {
                panel.getTextDimBlock().setForeground(Color.red);
            }
        } catch (NumberFormatException e) {
            existError[26] = true; //il valore inserito non è un numero
            existError[2] = false; //non è un numero quindi non puo essere <0
            existError[3] = false; //non è un numero quindi non puo essere troppo grande
            panel.getTextDimBlock().setForeground(Color.red);
        }
        writeError();
    }

    /**
     * Method that checks the file size and consistency with the partition
     */
    public void dimFile() {
        simId = panel.getSimId();
        existError[27] = false; // il valore inserito dovrebbe essere un numero
        try {
            int file = Integer.parseInt(panel.getTextDimFile().getText());
            String stringFileExp = panel.getComboDimFile();
            int fileExp = convertSize(stringFileExp);
            
            boolean correctValue = false;
            boolean consistence = false;
            if ((fileExp == 10 && file <= 1024)
                    || (fileExp == 20 && file <= 1024)
                    || (fileExp == 30 && file <= 1024)
                    || (fileExp == 40 && file <= 2)) {
                if (file > 0) {
                    correctValue = true;
                    existError[4] = false; // file inserito è corretto
                } else {
                    existError[4] = true; // valore <0
                }
                existError[5] = false; // file inserito corretto
            } else {
                existError[5] = true; // valore troppo grande
                existError[4] = false; // file inserito è corretto
            }

            int partition;
            try {
                partition = Integer.parseInt(panel.getTextDimPart().getText());
            } catch (NumberFormatException e) {
                partition = -1;
            }
            int partExp = convertSize(panel.getComboDimPart());
            long partSize = dimensionPow(partition, partExp);
            long fileSize = dimensionPow(file, fileExp);
            model.calculateMaxLenghtFile(simId);
            long maxLenght = model.getMaxLenghtFile(simId);
            if(maxLenght >= partSize)
                maxLenght=partSize*3/4;
            setMaxFile(maxLenght, stringFileExp);
            if (maxLenght >= fileSize) {
                existError[24] = false; //valore coerente con il disco
                existError[38] = false;
                consistence = true;
                panel.getTextDimFile().setForeground(Color.black);
                panel.getTextDimPart().setForeground(Color.black);
                panel.getTextDimBlock().setForeground(Color.black);
            } else {
                existError[38] = true; //valore non coerente con l'indirizzamento
                panel.getTextDimFile().setForeground(Color.red);
                panel.getTextDimBlock().setForeground(Color.red);
                panel.getTextDimPart().setForeground(Color.red);
            }
            if (correctValue && consistence) {
                panel.getTextDimFile().setForeground(Color.black);
            } else {
                panel.getTextDimFile().setForeground(Color.red);
            }
        } catch (NumberFormatException e) {
            existError[27] = true; //il valore inserito non è un numero
            existError[4] = false; //non è un numero quindi non puo essere <0
            existError[5] = false; //non è un numero quindi non puo essere troppo grande
            panel.getTextDimFile().setForeground(Color.red);
        }
        writeError();
    }

    /**
     * Method that checks the file size and consistency with the partition
     */
    public void dimMultiFile() {
        simId = panel.getSimId();
        existError[27] = false; // il valore inserito dovrebbe essere un numero
        try {

            int partition;
            try {
                partition = Integer.parseInt(panel.getTextDimPart().getText());
            } catch (NumberFormatException e) {
                partition = -1;
            }
            int partExp = convertSize(panel.getComboDimPart());
            long partSize = dimensionPow(partition, partExp);
            int file = Integer.parseInt(panel.getTextDimMultiFile().getText());
            String exp = panel.getComboDimMultiFile();
            int fileExp = convertSize(exp);
            boolean correctValue = false;
            boolean consistence = false;
            setFreeSpace(exp);
            if ((fileExp == 10 && file <= 1024)
                    || (fileExp == 20 && file <= 1024)
                    || (fileExp == 30 && file <= 1024)
                    || (fileExp == 40 && file <= 1024)) {
                if (file > 0) {
                    correctValue = true;
                    existError[4] = false; // file inserito è corretto
                } else {
                    existError[4] = true; // valore <0
                }
                existError[5] = false; // file inserito corretto
            } else {
                existError[5] = true; // valore troppo grande
                existError[4] = false; // file inserito è corretto
            }

            long fileSize = dimensionPow(file, fileExp);
            long freeSpace = partSize - busySpace;
            if (freeSpace > fileSize) {
                existError[37] = false; //file valido
                consistence = true;
            } else {
                existError[37] = true; //valore non coerente con il disco
                panel.getTextDimMultiFile().setForeground(Color.red);
            }
            if (correctValue && consistence) {
                panel.btAddFileSetEnabled(validMultiFile());
                panel.getTextDimMultiFile().setForeground(Color.black);
            } else {
                panel.btAddFileSetEnabled(false);
                panel.getTextDimMultiFile().setForeground(Color.red);
            }
        } catch (NumberFormatException e) {
            existError[27] = true; //il valore inserito non è un numero
            existError[4] = false; //non è un numero quindi non puo essere <0
            existError[5] = false; //non è un numero quindi non puo essere troppo grande
            panel.getTextDimMultiFile().setForeground(Color.red);
            panel.btAddFileSetEnabled(false);
        }
        writeError();
    }

    /**
     * Method that say current free space in the partition
     * @param exp
     */
    public void setFreeSpace(String exp) {
        int partition;
        try {
            partition = Integer.parseInt(panel.getTextDimPart().getText());
        } catch (NumberFormatException e) {
            partition = -1;
        }
        int partExp = convertSize(panel.getComboDimPart());
        long partSize = dimensionPow(partition, partExp);
        int fileExp = convertSize(exp);
        long value = (partSize - busySpace) / (long) (Math.pow(2, fileExp));
        panel.setLabelFreeDiskSpace(("" + value), exp);
    }

    /**
     * Method that calculate max file lenght that user can insert
     * in the simulation
     * @param maxLenght
     * @param exp
     */
    public void setMaxFile(long maxLenght, String exp) {
        int fileExp = convertSize(exp);
        long value = maxLenght / (long) (Math.pow(2, fileExp));
        panel.setLabelMaxFileDimension(("" + value), exp);
    }

    /**
     * Method that calls the model to include a file with multiple input mode
     */
    public void testAddFile() {
        simId = panel.getSimId();
        String fileName = panel.getTextFileName().getText();
        int file = Integer.parseInt(panel.getTextDimMultiFile().getText());
        String exp = panel.getComboDimMultiFile();
        int fileExp = convertSize(exp);
        long fileSize = dimensionPow(file, fileExp);
        busySpace = busySpace + fileSize;
        setFreeSpace(exp);
        model.setMultiFile(simId, fileName, file, fileExp);
        panel.btAddFileSetEnabled(false);
    }

    /**
     * Method that calls the model to remove a file with multiple input mode
     */
    public void testDeleteFile(String fileName, String fileValue) {
        simId = panel.getSimId();
        int stringSize = fileValue.length();
        int fileExp = convertSize(fileValue.substring(stringSize - 2, stringSize));
        int file = Integer.parseInt(fileValue.substring(0, stringSize - 3));
        long fileSize = dimensionPow(file, fileExp);
        busySpace = busySpace - fileSize;
        String exp = panel.getComboDimMultiFile();
        setFreeSpace(exp);
        model.deleteFile(simId, fileName); //da vedere l'average in questo caso l'inserimento funziona in modo diverso
    }

    /**
     * Method that reset error when File insert mode swicched
     */
    public void updateError() {
        existError[4] = false;
        existError[5] = false;
        existError[6] = false;
        existError[24] = false;
        existError[27] = false;
        existError[28] = false;
        writeError();
    }

    /**
     * Method that check if a file is valid (Single insert mode)
     */
    public void checkFile() {
        existError[27] = false; // il valore inserito dovrebbe essere un numero
        try {
            int partition;
            try {
                partition = Integer.parseInt(panel.getTextDimPart().getText());
            } catch (NumberFormatException e) {
                partition = -1;
            }
            int partExp = convertSize(panel.getComboDimPart());
            long partSize = dimensionPow(partition, partExp);
            
            int file = Integer.parseInt(panel.getTextDimFile().getText());
            String stringFileExp = panel.getComboDimFile();
            int fileExp = convertSize(stringFileExp);
            long fileSize = dimensionPow(file, fileExp);

            model.calculateMaxLenghtFile(simId);
            long maxLenght = model.getMaxLenghtFile(simId);
            if(maxLenght > partSize) 
                maxLenght = partSize*3/4;
            
            setMaxFile(maxLenght, stringFileExp);
            if (maxLenght >= fileSize) {
                existError[38] = false;
                panel.getTextDimFile().setForeground(Color.black);
            } else {
                existError[38] = true; //valore non coerente con l'indirizzamento
                panel.getTextDimFile().setForeground(Color.red);
            }
        } catch (NumberFormatException e) {
            existError[27] = true; //il valore inserito non è un numero
            existError[4] = false; //non è un numero quindi non puo essere <0
            existError[5] = false; //non è un numero quindi non puo essere troppo grande
            panel.getTextDimFile().setForeground(Color.red);
        }
        writeError();

    }

    /**
     * Method that check if a file is valid (Multiple insert mode)
     * @return
     */
    public boolean validMultiFile() {
        String name = panel.getFileName().getText();
        boolean exist = false;
        boolean valid = false;
        for (int index = 0; index < panel.getInsertedFileTable().getRowCount() && !exist; index++) {
            if (name.equals(panel.getInsertedFileTable().getValueAt(index, 0))) {
                exist = true;
            }
        }
        int partition;
        try {
            partition = Integer.parseInt(panel.getTextDimPart().getText());
        } catch (NumberFormatException e) {
            partition = -1;
        }
        int partExp = convertSize(panel.getComboDimPart());
        long partSize = dimensionPow(partition, partExp);
        int file;
        try {
            file = Integer.parseInt(panel.getTextDimMultiFile().getText());
        } catch (NumberFormatException e) {
            file = -1;
        }
        String exp = panel.getComboDimMultiFile();
        int fileExp = convertSize(exp);
        long fileSize = dimensionPow(file, fileExp);
        long freeSpace = partSize - busySpace;
        if (fileSize > 0 && freeSpace > fileSize) {
            valid = true;
        }
        if (!exist && valid) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Method that return if a specific error is in the simulation
     * @param x
     * @return
     */
    public boolean getExistError(int x) {
        return existError[x];
    }

    /**
     * Method that change File System type
     * @param newType
     */
    public void changeFileSystemType(String newType) {
        model.switchFileSystem(simId, newType);
        panel.readSimulationValues();
    }

    /**
     * Method that set the value of busy space
     * @param space
     * @param exp
     */
    public void setBusySpace(int space, String exp) {
        busySpace = busySpace + dimensionPow(space, convertSize(exp));
    }
}
